<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>港航大模型</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background: #f0f2f5;
        }

        .chat-container {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .chat-messages {
            height: 400px;
            overflow-y: auto;
            border-radius: 8px;
            padding: 15px;
            background: #f8f9fa;
            margin-bottom: 20px;
        }

        .message {
            margin: 10px 0;
            display: flex;
            align-items: flex-start;
        }

        .user-message {
            align-self: flex-end;
        }

        .message-content {
            max-width: 70%;
            padding: 12px;
            border-radius: 15px;
            background: #e1f5fe;
            color: #1a73e8;
            word-break: break-all;
        }

        .system-message .message-content {
            background: #e8f5e1;
            color: #256028;
        }

        .image-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 10px;
        }

        .image-item {
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .image-item img {
            width: 100%;
            height: auto;
            display: block;
        }

        .chat-input {
            display: flex;
            align-items: center;
            padding: 10px 0;
        }

        .input-box {
            flex: 1;
            margin-right: 10px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 20px;
            font-size: 16px;
        }

        .btn {
            padding: 10px 20px;
            background: #1a73e8;
            color: white;
            border: none;
            border-radius: 20px;
            cursor: pointer;
            font-size: 16px;
        }

        .btn-clear {
            background: #e84c3d;
            margin-left: 10px;
        }

        .loader {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #1a73e8;
            border-radius: 50%;
            width: 24px;
            height: 24px;
            animation: spin 1s linear infinite;
            display: inline-block;
            margin: 5px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .error-message {
            color: red;
            margin-top: 10px;
        }

        /* 新增模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.5);
            z-index: 1000;
        }

        .modal-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
        }

        .close-btn {
            cursor: pointer;
            float: right;
            font-size: 24px;
            margin-right: 10px;
        }

        iframe#htmlViewer {
            width: 100%;
            height: 600px;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-messages" id="chatMessages"></div>

        <div class="chat-input">
            <input type="text" class="input-box" id="userInput" placeholder="输入问题...">
            <button class="btn" onclick="sendMessage()">发送</button>
            <button class="btn btn-clear" onclick="clearChat()">清空聊天</button>
        </div>
    </div>

    <!-- 新增模态框 -->
    <div id="modal" class="modal">
        <div class="modal-content">
            <span class="close-btn">&times;</span>
            <iframe id="htmlViewer" src="" frameborder="0"></iframe>
        </div>
    </div>

    <script>
        // 接口配置区域（需替换）
        const QA_API_URL = 'http://localhost:3000/LLM'; // 问答接口地址
        const IMG_API_URL = 'YOUR_IMAGE_API_ENDPOINT'; // 图片接口地址

        // 消息容器
        const chatMessages = document.getElementById('chatMessages');
        const userInput = document.getElementById('userInput');

        // 发送消息
        function sendMessage() {
            const message = userInput.value.trim();
            if (!message) return;

            // 清空输入框
            userInput.value = '';

            // 添加用户消息
            addMessage('user', message);

            // 开始加载状态
            showLoader();

            // 判断是否需要图片接口
            if (message.includes('图片')) {
                fetchImage(message);
            } else {
                fetchAnswer(message);
            }
        }

        // 获取答案
        function fetchAnswer(question) {
            fetch(QA_API_URL, {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({ question })
            })
            .then(response => response.json())
            .then(data => {
                hideLoader();
                const htmlPath = data.html_content || null;
                addMessage('system', data.answer || '未找到答案', htmlPath);
            })
            .catch(error => {
                hideLoader();
                addMessage('system', '请求失败，请重试');
            });
        }

        // 获取图片
        function fetchImage(keyword) {
            fetch(IMG_API_URL, {
                method: 'POST',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify({ keyword })
            })
            .then(response => response.json())
            .then(data => {
                hideLoader();
                if (data.images && data.images.length) {
                    addImageMessages(data.images);
                } else {
                    addMessage('system', '没有找到相关图片');
                }
            })
            .catch(error => {
                hideLoader();
                addMessage('system', '图片请求失败，请重试');
            });
        }

        // 添加文本消息
        function addMessage(type, content, htmlPath) {
            const msgDiv = document.createElement('div');
            msgDiv.className = `message ${type}-message`;
            msgDiv.innerHTML = `
                <div class="message-content">${content}</div>
            `;
            if (htmlPath) {
                const button = document.createElement('button');
                button.className = 'btn btn-html';
                button.textContent = '打开图表';
                button.onclick = function() { openHTML(htmlPath); };
                msgDiv.appendChild(button);
            }
            chatMessages.appendChild(msgDiv);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        // 添加图片消息
        function addImageMessages(urls) {
            const imageContainer = document.createElement('div');
            imageContainer.className = 'message system-message';
            imageContainer.innerHTML = `
                <div class="image-container">
                    ${urls.map(url => `
                        <div class="image-item">
                            <img src="${url}" alt="图片">
                        </div>
                    `).join('')}
                </div>
            `;
            chatMessages.appendChild(imageContainer);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        // 显示加载状态
        function showLoader() {
            const loader = document.createElement('div');
            loader.className = 'loader';
            chatMessages.appendChild(loader);
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        // 隐藏加载状态
        function hideLoader() {
            const loader = chatMessages.querySelector('.loader');
            if (loader) loader.remove();
        }

        // 清空聊天记录
        function clearChat() {
            chatMessages.innerHTML = '';
            userInput.value = '';
        }

        // 打开HTML文件
        function openHTML(path) {
            const modal = document.getElementById('modal');
            const iframe = document.getElementById('htmlViewer');
            iframe.src = path;
            modal.style.display = 'block';
        }

        // 关闭模态框事件
        document.addEventListener('click', function(e) {
            if (e.target.matches('.close-btn')) {
                const modal = document.getElementById('modal');
                modal.style.display = 'none';
            }
        });
    </script>
</body>
</html>